#include "ts.h"


int ts_init()
{
	int fd_ts;
	fd_ts = open(TS_PATH,O_RDWR);
	if(fd_ts < 0)
	{
		printf("ts_open failed!\n");
		return -1;
	}	
	return fd_ts;
}

void ts_trace(int ts, struct coordinate *coor, bool *released)
{
	struct input_event buf;
	*released = false;
	bool x_ready = false;
	bool y_ready = false;

	while(1)
	{
		bzero(&buf, sizeof(buf));
		read(ts, &buf, sizeof(buf));

		if(buf.type == EV_ABS &&
		   buf.code == ABS_PRESSURE &&
		   buf.value == 0)
		{
			*released = true;
			break;
		}

		if(buf.type == EV_ABS &&
		   buf.code == ABS_X)
		{
			coor->x = buf.value;
			x_ready = true;
		}

		if(buf.type == EV_ABS &&
		   buf.code == ABS_Y)
		{
			coor->y = buf.value;
			y_ready = true;
		}

		if(x_ready && y_ready)
			break;
	}
}